package tgc.edu.ljc.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.ljc.custom.AjaxResult;
import tgc.edu.ljc.custom.CommonController;
import tgc.edu.ljc.entity.DataSum;
import tgc.edu.ljc.service.DataSumService;
import tgc.edu.ljc.web.form.DataSumForm;

@Controller
@RequestMapping(value="/dataSum")
public class DataSumController extends CommonController<DataSum, Integer, DataSumForm> {

	@Autowired
	private DataSumService dataSumService;

	@Override
	public void edit(DataSumForm form, ModelMap map) throws InstantiationException, IllegalAccessException {
		DataSum model = new DataSum();
		Integer id = form.getId();
		Integer parentId = form.getParentId();	//父级id
		if(id != null) {
			model = dataSumService.findById(id);
			parentId = model.getParentId();
		}
		if(parentId != null) {	//父级id
			DataSum parent = dataSumService.findById(parentId);
			model.setParent(parent);
		}
		map.put("model", model);
	}

	@Override
	public Object save(DataSumForm form) {
		try {
			DataSum model = new DataSum();
			Integer id = form.getId();
			if(id != null) {
				model = dataSumService.findById(id);
			}
			//父级菜单id
			Integer parentId = form.getParentId();
			if(parentId == null) {
				model.setParent(null);
			}else {
				model.setParent(new DataSum(parentId));
			}
			BeanUtils.copyProperties(form, model,"id","role","parent");
			dataSumService.save(model);
			return new AjaxResult("数据保存成功！");
		} catch (Exception e) {
			return new AjaxResult(false,"数据保存失败");
		}
	}
	
	/***
	 * 树形加载
	 * @return
	 */
	@RequestMapping(value="/treedata")
	@ResponseBody
	public Object treedata() {
		Specification<DataSum> spec = buildSpec1();
		List<DataSum> list = dataSumService.findAll(spec);
		return buildTree(list,null);
		
		
		
	}
	
	private Object buildTree(List<DataSum> list, Integer id) {
		List<HashMap<String, Object>> result=new ArrayList<>();
		for (DataSum dept : list) {
			if(dept.getId()!=id) {
				HashMap<String, Object> node=new HashMap<>();
				node.put("id", dept.getId());
				node.put("text",dept.getName());
				node.put("pid", dept.getParentId());
				node.put("nodes",buildTree(dept.getChildren(), id));
				result.add(node);
			}
		}
		return result;
	}
	
	public Specification<DataSum> buildSpec1() {
		Specification<DataSum> specification = new Specification<DataSum>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<DataSum> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
					Predicate parent = cb.isNull(root.get("parent"));
					rules.add(parent);
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}
	
	
}
